package com.heihei.douyinvideotrans;

import com.heihei.util.FileUtilF;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber;

import java.io.File;
import java.io.IOException;

public class DouYinVideoTransStart {
    public static void main(String[] args) {
        start();
    }

    public static void start() {
        System.out.println("请输入源目录：");
        File validSrcDir = FileUtilF.getValidSrcDir();
        startTransDouyinVideo(validSrcDir);
    }


    public static void startTransDouyinVideo(File parentDir) {
        StringBuilder sb = new StringBuilder();
        File transoutDir = new File(parentDir, "transout");
        if(!transoutDir.exists()) {
            transoutDir.mkdirs();
        }
        for (File file : parentDir.listFiles()) {
            if(FileUtilF.isVideo(file)) {
                String targetFileName = getTargetFileName(file);
                String videoFileLenStr = getVideoFileLenStr(file);
                sb.append("ffmpeg -to " + videoFileLenStr + "  -accurate_seek -i \"" + file.getName() + "\" -c copy -avoid_negative_ts 1  \"transout/" + targetFileName + "\"")
                        .append("\r\n");
            }
        }
        sb.append("pause");
        File targetFile = new File(parentDir, "startTransDouyinVideo.cmd");
        try {
            FileUtilF.writeTxtFile(sb.toString(), targetFile, "GBK");
            System.out.println("OK");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static String getTargetFileName(File file) {
        String oriName = file.getName();
        String newName = oriName.substring(0, 19);
        newName = newName.replaceAll("-", "");
        newName = newName.replaceAll(" ", "_");
        return newName + ".mp4";
    }


    public static String getVideoFileLenStr(File videoFile) {
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoFile);
        try {
            frameGrabber.start();
            long duration = frameGrabber.getLengthInTime() / 1000000; // 转换为秒
//            System.out.println("视频时长：" + duration + "秒");
            //转换为时分秒
            long second = duration % 60;
            long totalMinute = duration / 60;
            long minute = totalMinute % 60;
            long hour = totalMinute / 60;
            String videoLen = buildLenStr(hour) + ":" + buildLenStr(minute) + ":" + buildLenStr(second);
//            System.out.println("视频时长：" + videoLen);
            return videoLen;

        } catch (FrameGrabber.Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                frameGrabber.stop();
            } catch (FrameGrabber.Exception e) {
                throw new RuntimeException(e);
            }
        }
    }



    public static String buildLenStr(long num) {
        return buildLenStr(num, 2);
    }

    public static String buildLenStr(long num, int length) {
        StringBuilder sb = new StringBuilder(String.valueOf(num));
        int sbLen = sb.length();
        if(sbLen < length) {
            int addLen = length - sbLen;
            for (int i = 0; i < addLen; i++) {
                sb.insert(0, "0");
            }
        }
        return sb.toString();
    }
}
